package com.doge.calcalbum.business.home

import android.content.Context
import android.os.Environment
import androidx.lifecycle.MutableLiveData
import com.bumptech.glide.Glide
import com.doge.calcalbum.business.account.AccountHelper
import com.doge.calcalbum.core.base.LoadStatus
import com.doge.calcalbum.utils.appContext
import com.doge.calcalbum.utils.isMainThread
import com.doge.calcalbum.utils.runOnIO
import com.doge.calcalbum.utils.runOnUI
import java.io.File
import java.math.BigDecimal

object CacheUtils {
    val loading = MutableLiveData<LoadStatus>()

    fun calcTotalSize(context: Context): String? {
        try {
            var cacheSize = getFolderSize(context.cacheDir)
            if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
                cacheSize += getFolderSize(context.externalCacheDir)
            }
            return getFormatSize(cacheSize.toDouble())
        } catch (e: Exception) {
            e.printStackTrace()
        }


        return ""
    }

    fun clear(context: Context) {
        require(isMainThread())
        loading.value = LoadStatus.LOADING
        runOnIO {
            Glide.get(appContext).clearDiskCache()
            deleteDir(context.cacheDir)
            if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
                deleteDir(context.externalCacheDir)
            }
            runOnUI {
                loading.value = LoadStatus.SUCCESS
                loading.value = LoadStatus.IDLE
            }
        }
    }

    private fun getFormatSize(size: Double): String? {
        val kiloByte = size / 1024
        val megaByte = kiloByte / 1024
        val gigaByte = megaByte / 1024
        if (gigaByte < 1) {
            val result2 = BigDecimal(java.lang.Double.toString(megaByte))
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
                .toPlainString() + "MB"
        }
        val teraBytes = gigaByte / 1024
        if (teraBytes < 1) {
            val result3 = BigDecimal(java.lang.Double.toString(gigaByte))
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
                .toPlainString() + "GB"
        }
        val result4 = BigDecimal.valueOf(teraBytes)
        return (result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
                + "TB")
    }

    private fun deleteDir(dir: File?): Boolean {
        if (dir != null && dir.isDirectory) {
            val children = dir.list()
            for (child in children) {
                val success = deleteDir(File(dir, child))
                if (!success) {
                    return false
                }
            }
            return dir.delete()
        }
        return dir?.delete() ?: false
    }

    private fun getFolderSize(file: File?): Long {
        var size: Long = 0
        if (file != null) {
            val fileList = file.listFiles()
            if (fileList != null && fileList.isNotEmpty()) {
                for (value in fileList) {
                    size = if (value.isDirectory) {
                        size + getFolderSize(value)
                    } else {
                        size + value.length()
                    }
                }
            }
        }
        return size
    }
}